package com.example.fbi_text.Acitivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fbi_text.R;

import com.example.fbi_text.bean.Product;
import com.example.fbi_text.database.DBHelper;

//添加商品
public class AddGoodsActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int REQUEST_IMAGE_CAPTURE = 1;
    private static final int REQUEST_PICK_IMAGE = 2;
    private ImageView add_goods_back;
    private Button buttonChooseFromGallery;
    private ImageView imageView;
    private DBHelper dbHelper;
    private EditText goods_name;
    private EditText goods_price;
    private EditText goods_sales;
    private EditText goods_quantity;
    private EditText goods_content;
    private Button add_goods_button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_goods);
        add_goods_back = findViewById(R.id.add_goods_back);
        buttonChooseFromGallery = findViewById(R.id.buttonChooseFromGallery);
        imageView = findViewById(R.id.imageView);
        goods_name = findViewById(R.id.et_goods_name);
        goods_price = findViewById(R.id.et_goods_price);
        goods_content = findViewById(R.id.et_goods_content);
        add_goods_button = findViewById(R.id.add_goods_button);
        dbHelper = DBHelper.getInstance(this);

        add_goods_back.setOnClickListener(this);
        buttonChooseFromGallery.setOnClickListener(this);
        add_goods_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.add_goods_back) { // 返回
            finish();
        } else if (id == R.id.buttonChooseFromGallery) { // 相册选择图片点击事件
            openGallery();
        } else if (id == R.id.add_goods_button) { // 添加商品
            String name = goods_name.getText().toString();
            String price = goods_price.getText().toString();
            String content = goods_content.getText().toString();

            if (allInputNotEmpty(name, price, content)) {
                Product product = new Product();
                product.setName(name);
                product.setPrice(price);
                product.setDescription(content);
                int productId = DBHelper.getInstance(this).insertGoods2(product);
                if (productId >= 0) {
                    Toast.makeText(this, "添加商品成功，可继续添加", Toast.LENGTH_SHORT).show();
                    goods_name.setText("");
                    goods_price.setText("");
                    goods_content.setText("");
                } else {
                    Toast.makeText(this, "添加商品失败，请检查输入", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "所有字段不能为空", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //相册选择图片
    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_PICK_IMAGE);
    }

    //返回图片 URI
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri selectedImage = data.getData();
            imageView.setImageURI(selectedImage);
        }
    }

    //输入不能为空
    private boolean allInputNotEmpty(String... inputStrings) {
        for (String input : inputStrings) {
            if (TextUtils.isEmpty(input)) {
                return false; // 如果输入内容为空返回 false
            }
        }
        return true;
    }

}